package com.ygj.employee.action;

import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.persistent.model.Employee;
import com.base.common.persistent.model.Terminal;
import com.base.common.utils.EasyUtil;
import com.cjy.common.bean.OpAttributes;
import com.cjy.user.service.UserService;
import com.gexin.rp.sdk.base.uitls.Base64Util;
import com.ygj.inspection.service.EmployeeService;

@Action("employeeAction")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class EmployeeAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	@Resource
	private UserService userService;

	@Resource
	private EmployeeService employeeService;
	
	private String ids;	// employee表主键id

	private String code; // 设备码
	
	private String name; 	// 用户名

	private String phone; // 手机号

	private int captcha; // 验证码

	private int compoundsId; // 小区id

	private int dept; // 职位信息

	private int bid = 0; // 0,android城际云; 1,android城管家; 2,微信城际云; 3,ios城际云;
							// 4,ios云管家
	private String faceData;
	
	public void setFaceData(String faceData) {
		this.faceData = faceData;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public int getBid() {
		return bid;
	}

	public void setBid(int bid) {
		this.bid = bid;
	}

	public int getCaptcha() {
		return captcha;
	}

	public void setCaptcha(int captcha) {
		this.captcha = captcha;
	}

	public int getDept() {
		return dept;
	}

	public void setDept(int dept) {
		this.dept = dept;
	}

	/**
	 * @return the ids
	 */
	public String getIds() {
		return ids;
	}

	/**
	 * @param ids the ids to set
	 */
	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public int getCompoundsId() {
		return compoundsId;
	}

	public void setCompoundsId(int compoundsId) {
		this.compoundsId = compoundsId;
	}

	/**
	 * 云管家注册
	 * 
	 * @return
	 */
	public String employeeRegiste() {
		try {
			Terminal terminal = new Terminal();

			if (bid == 1) {
				terminal.setPhone(EasyUtil.rc4(phone, "szcjy").trim());
				terminal.setCode(code.trim());
			} else if (bid == 4) {
				terminal.setPhone(phone.trim());
				terminal.setAppleId(code.trim());
			}
			terminal.setBid(bid);
			int result = userService.managerRegiste(terminal);
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", result));
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 云管家登录
	 * 
	 * @return
	 */
	public String employeeLogin() {
		try {
			Terminal terminal = new Terminal();

			if (bid == 4 || bid == 6) {
				terminal.setAppleId(code);
			} else {
				terminal.setCode(code);
			}
			terminal.setBid(bid);
			List employees = userService.login(terminal);
			if (employees.size() > 0) {
				session.put("users", employees);
				actionResult = ActionResult.getActionResult(new OpAttributes()
						.add("code", 1).add("result", employees));
			} else {
				actionResult = ActionResult.getActionResult(new OpAttributes()
						.add("code", 0));
			}
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 根据部门职位获取员工列表
	 * 
	 * @return
	 */
	public String getEmployeesByDept() {
		try {

			List<Employee> employees = employeeService
					.getEmployeesByDeptAndCompoundsId(dept, compoundsId);
			/*for (int i = 0; i < employees.size(); i++) {
				if (employees.get(i).getId() == ((List<Employee>) request
						.getSession().getAttribute("users")).get(0).getId()) {
					employees.remove(i);
				}
			}*/
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 1).add("result", employees));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}
	
	public String getYunWorkEmployeeByCondition() {
		try {
			Map map = new HashMap();
			map.put("name", name);
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1)
					.add("result", employeeService.getYunWorkEmployeeByCondition(map)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes("code", 0));
		}
		return returnType();
	}
	
	public String getEmployeesByUncertainName() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1)
					.add("result", employeeService.getEmployeesByUncertainName(compoundsId, name, dept)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes("code", 0));
		}
		return returnType();
	}

	/**
	 * 员工选择默认小区
	 * 
	 * @return
	 */
	public String chooseDefaultCompounds() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", employeeService.chooseDefaultCompounds(
							((Employee) session.get("users")).getId(),
							compoundsId)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}
	
	public String getHeadPicByMySelf() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result", new OpAttributes().add("headPicUrl", employeeService.getHeadPicUrlByMySelf(((List<Employee>)request.getSession().getAttribute("users")).get(0).getId()))));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}
	
	/**
	 * 获取环信用户信息
	 * 
	 */
	public String getEmchatUserInfo() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result", employeeService.getEmchatUserInfo(ids)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}
	
	public String getFaceData() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result", employeeService.getFaceData(ids)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		
		return returnType();
	}
	
	public String updateFaceData() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", employeeService.updateFaceData(ids, faceData)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

}
